package com.angel.provider.health.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.Constants;
import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.StringUtils;
import com.angel.dao.health.BuzDoctorMapper;
import com.angel.dao.health.SysAreaMapper;
import com.angel.health.BuzDoctor;
import com.angel.health.SysArea;
import com.angel.health.ln.ChartMap;
import com.angel.health.ln.WangYi;
import com.angel.provider.app.WangYiProvider;
import com.angel.provider.health.BuzDoctorProvider;

@Service
public class BuzDoctorProviderImpl extends BaseProviderImpl<BuzDoctor> implements BuzDoctorProvider {

	@Autowired
	private BuzDoctorMapper mapper;
	@Autowired
	private WangYiProvider wyProvider;
	@Autowired
	private SysAreaMapper areaMapper;
	
	//根據id查詢
	public BuzDoctor findById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	//保存
	public Map<String, String> save(BuzDoctor doctor) {
		Map<String, String> map = new HashMap<String, String>();
		if(doctor == null || doctor.getName() == null || doctor.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		if(StringUtils.isEmpty(doctor.getMobile())){
			map.put("msg", "手机号不能为空");
			map.put("code", "0");
			return map;
		}
		BuzDoctor doctor1 = new BuzDoctor();
		doctor1.setMobile(doctor.getMobile());
		List<Map<String, Object>> doctorAll = mapper.findDoctorAll(doctor1);
		if(doctorAll != null && doctorAll.size() > 0){
			map.put("msg", "手机号已存在");
			map.put("code", "0");
			return map;
		}
		doctor.setCreateTime(new Date());
		doctor.setDelFlag(1);
		doctor.setStatus(0);
		mapper.insert(doctor);
		//生产编码
		Integer provinces = doctor.getProvinces();
		Integer city = doctor.getCity();
		String areaCode = "000000";
		SysArea area = new SysArea();
		if(city != null && city != 0){
			area.setId(city);
			List<Map<String, Object>> areaList = areaMapper.findAll(area);
			if(areaList != null && areaList.size() > 0){
				areaCode = (String) areaList.get(0).get("code");
			}
		}else if(provinces != null && provinces != 0){
			area.setId(provinces);
			List<Map<String, Object>> provincesList = areaMapper.findAll(area);
			if(provincesList != null && provincesList.size() > 0){
				areaCode = (String) provincesList.get(0).get("code");
			}
		}
		String code = CodeUtil.createCode(areaCode, Constants.DOCTOR, doctor.getId());
		doctor.setCode(code);
		mapper.updateByPrimaryKeySelective(doctor);
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//审核
	public Map<String, String> update(BuzDoctor doctor) {
		Map<String, String> map = new HashMap<String, String>();
		mapper.updateByPrimaryKeySelective(doctor);
		BuzDoctor buzDoctor = mapper.selectByPrimaryKey(doctor.getId());
		//如果审核成功则将医师信息提交到用户表
//		SysUser su = new SysUser();
//		BeanUtils.copyProperties(buzDoctor, su);
//		su.setPassword(buzDoctor.getPassword());
//		su.setId(null);
		//su.setId(null);
		//云信生成token时，医师注册使用"_"+phone做的accid
		String json = JsonUtil.toJson(new WangYi("_"+buzDoctor.getMobile(),buzDoctor.getName()));
		try {
			String createUser = wyProvider.createUser(json);
			if(!StringUtils.isEmpty(createUser)){
				Map<String, String> map1 = JsonUtil.jsonStringToMap(createUser);
				if("200".equals(map1.get("code"))){
					buzDoctor.setToken(map1.get("token"));
					mapper.updateByPrimaryKeySelective(buzDoctor);
//					su.setToken(map1.get("token"));
//					sysUserProvider.save(su,"21");
					map.put("msg", "审核成功");
					map.put("code", "1");
					return map;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		map.put("msg", "审核失败");
		map.put("code", "0");
		BuzDoctor doctor1 = new BuzDoctor();
		doctor1.setId(doctor.getId());
		doctor1.setStatus(2);
		doctor1.setCheckFailMsg("云信注册失败");
		mapper.updateByPrimaryKeySelective(doctor1);
		return map;
	}

	//批量刪除
	public int delete(String ids) {
		try {
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				BuzDoctor doctor = new BuzDoctor();
				doctor.setId(Integer.parseInt(id));
				doctor.setDelFlag(0);
				mapper.updateByPrimaryKeySelective(doctor);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	//刪除單個
	public int deleteById(BuzDoctor doctor) {
		doctor.setDelFlag(0);
		return mapper.updateByPrimaryKeySelective(doctor);
	}

	@Override
	public List<Map<String, Object>> findDoctorAll(BuzDoctor doctor) {
		// TODO Auto-generated method stub
		return mapper.findDoctorAll(doctor);
	}

	@Override
	public List<Map<String, Object>> findDoctorByPhone(BuzDoctor doctor) {
		// TODO Auto-generated method stub
		BuzDoctor bd = new BuzDoctor();
		bd.setMobile(doctor.getMobile());
		return mapper.findDoctorAll(bd);
	}

	@Override
	public List<ChartMap> loadChart() {
		// TODO Auto-generated method stub
		return mapper.loadChart();
	}

	//修改
	public void updateDoc(BuzDoctor doctor) {
		mapper.updateByPrimaryKeySelective(doctor);
	}

}
